// checkout.js - Vuex store module for checkout functionality

const state = {
  checkoutItems: []
}

const mutations = {
  SET_CHECKOUT_ITEMS(state, items) {
    state.checkoutItems = items
  },
  CLEAR_CHECKOUT_ITEMS(state) {
    state.checkoutItems = []
  }
}

const actions = {
  setCheckoutItems({ commit }, items) {
    commit('SET_CHECKOUT_ITEMS', items)
  },
  clearCheckoutItems({ commit }) {
    commit('CLEAR_CHECKOUT_ITEMS')
  }
}

const getters = {
  checkoutItemCount: state => state.checkoutItems.length,
  checkoutTotal: state => {
    return state.checkoutItems.reduce((total, item) => {
      return total + (item.price * item.quantity)
    }, 0).toFixed(2)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
} 